package com.ruan.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruan.pojo.Result;
import com.ruan.pojo.User;
import com.ruan.query.UserQuery;
import com.ruan.service.UserService;
import com.ruan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:UserController
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/14 15:05
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserInfoById/{id}")
    @SentinelResource(value = "getUserInfoById-api",blockHandler = "block2")
    public UserVo getUserInfoById(@PathVariable Integer id){

        return userService.getUserInfoById(id);

    }

    public UserVo block2(@PathVariable Integer id, BlockException exception){
        UserVo userVo = new UserVo();
        userVo.setGender(1);
        userVo.setUserName("默认用户");
        return userVo;

    }


    //用户分页模糊查询
    @RequestMapping("/getUserList")
    @SentinelResource(value = "getUserList-api",blockHandler = "block")
    public PageInfo<UserVo> getUserList(UserQuery userQuery){

        //开启分页
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());

        List<UserVo> userVoList = userService.getUserList(userQuery);

        PageInfo<UserVo> pageInfo = new PageInfo<>(userVoList);

        return pageInfo;

    }

    public PageInfo<UserVo> block(UserQuery userQuery, BlockException exception){
        List<UserVo> userVoList =new ArrayList<>();
        UserVo userVo =new UserVo();
        userVo.setRoleName("默认用户");
        userVo.setGender(1);
        userVoList.add(userVo);
        PageInfo<UserVo> pageInfo = new PageInfo<>(userVoList);
        return pageInfo;
    }


    @PostMapping("/addUser")
    public Result addUser(User user){

        return userService.addUser(user);

    }

    @RequestMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable Integer id){

        return userService.deleteUserById(id);

    }

    @PostMapping("/editUser")
    public Result editUser(User user){

        return userService.editUser(user);

    }

    @RequestMapping("/deleteUserByIds")
    public Result deleteUserByIds(Integer[] ids){

        System.err.println(ids);
        return userService.deleteUserByIds(ids);

    }

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){

        return userService.getUserById(id);

    }

}
